<template>
  <div class="DingApprove">
    <el-form ref="form" :model="formInline" label-width="100px" class="custom-form">
      <el-form-item label="申请人">
        <el-input
          readonly
          class="custom-input"
          v-model="formInline.applicant"
          placeholder="请输入申请人姓名"
        ></el-input>
      </el-form-item>
      <el-form-item label="货物名称">
        <el-input readonly class="custom-input" v-model="formInline.goods" placeholder="请输入货物名称"></el-input>
      </el-form-item>
      <el-form-item label="运单总数量">
        <el-input
          readonly
          class="custom-input"
          v-model="formInline.bill_number"
          placeholder="请输入运单总数量"
        >
          <template slot="append">
            <span style="font-weight: 600;font-size: 14px;color: #555;">单</span>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="运单总重量">
        <el-input readonly class="custom-input" v-model="formInline.weight" placeholder="请输入运单总重量">
          <template slot="append">
            <span style="font-weight: 600;font-size: 14px;color: #555;">吨</span>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="运单总金额">
        <el-input readonly class="custom-input" v-model="formInline.money" placeholder="请输入运单总金额">
          <template slot="append">
            <span style="font-weight: 600;font-size: 14px;color: #555;">元</span>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="运单id">
        <el-input readonly class="custom-input" v-model="formInline.yund_id" placeholder="请输入运单id"></el-input>
      </el-form-item>
      <el-form-item label="公司名称">
        <el-input readonly class="custom-input" v-model="formInline.company" placeholder="请输入公司名称"></el-input>
      </el-form-item>
      <el-form-item label style="text-align: center;">
        <el-button @click="$emit('closeDialog')">取消</el-button>
        <el-button type="primary" @click="sure">审批</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Big from "big.js";
import { getUserInfo } from "@/utils/auth";
import {approveDing} from "@/api/childCompany/moneyManagment";
export default {
  name: "DingApprove",
    props: {
      form: {
        type: Array,
        default: []
      }
    },
  data() {
    return {
      formInline: {
        applicant: "",
        goods: "",
        bill_number: "",
        weight: "",
        money: "",
        yund_id: "",
        company: ""
      }
    };
  },
  mounted() {
    this.initial();
  },
  methods: {
    async initial() {
      this.formInline.applicant = getUserInfo().name;
      this.formInline.goods = [
        ...new Set(this.form.map(item => item.goods))
      ].join(",");
      this.formInline.weight = this.form.reduce((p, c) => {
        let shkg = new Big(c.shkg);
        let _p = new Big(p);
        return _p.plus(shkg).toNumber();
      }, 0);
      this.formInline.money = this.form.reduce((p, c) => {
        let zyf = new Big(c.zyf);
        let _p = new Big(p);
        return _p.plus(zyf).toNumber();
      }, 0);
      this.formInline.bill_number = this.form.length;
      this.formInline.yund_id = this.form.map(item => item.yund_id).join(",");
      this.formInline.company = this.form[0].company_name;
    },
    async sure() {
        let result = await approveDing(this.formInline);
        if(result.code == 200){
            this.$message.success('发起审批成功！');
            setTimeout(()=>{
                this.$emit('closeDialog');
            },1000)
        }
    }
  }
};
</script>

<style lang="scss" scoped>
.DingApprove {
  padding-right: 80px;
}
</style>
